package jnpf.base.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jnpf.base.ActionResult;
import jnpf.base.Page;
import jnpf.base.entity.SystemEntity;
import jnpf.base.model.base.SystemCrModel;
import jnpf.base.model.base.SystemListVO;
import jnpf.base.model.base.SystemVO;
import jnpf.base.service.SystemService;
import jnpf.base.vo.ListVO;
import jnpf.constant.MsgCode;
import jnpf.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统控制器
 *
 * @author ：JNPF开发平台组
 * @version: V3.1.0
 * @copyright 引迈信息技术有限公司
 * @date ：2022/6/21 15:33
 */
@Api(tags = "系统", value = "system")
@RestController
@RequestMapping("/api/system/System")
public class SystemController {

    @Autowired
    private SystemService systemService;

    /**
     * 获取系统列表
     *
     * @param page 关键字
     * @return ignore
     */
    @ApiOperation("获取系统列表")
    @GetMapping
    public ActionResult<ListVO<SystemListVO>> list(Page page) {
        List<SystemEntity> list = systemService.getList(page.getKeyword(), false);
        List<SystemListVO> jsonToList = JsonUtil.getJsonToList(list, SystemListVO.class);
        ListVO listVO = new ListVO();
        listVO.setList(jsonToList);
        return ActionResult.success(listVO);
    }

    /**
     * 获取系统详情
     *
     * @param id
     * @return ignore
     */
    @ApiOperation("获取系统详情")
    @GetMapping("/{id}")
    public ActionResult<SystemVO> info(@PathVariable("id") String id) {
        SystemEntity entity = systemService.getInfo(id);
        if (entity == null) {
            return ActionResult.fail(MsgCode.FA001.get());
        }
        SystemVO jsonToBean = JsonUtil.getJsonToBean(entity, SystemVO.class);
        return ActionResult.success(jsonToBean);
    }

    /**
     * 新建系统
     *
     * @param systemCrModel 新建模型
     * @return ignore
     */
    @ApiOperation("新建系统")
    @PostMapping
    public ActionResult create(@RequestBody SystemCrModel systemCrModel) {
        SystemEntity entity = JsonUtil.getJsonToBean(systemCrModel, SystemEntity.class);
        if (systemService.isExistFullName(entity.getId(), entity.getFullName())) {
            return ActionResult.fail(MsgCode.EXIST001.get());
        }
        if (systemService.isExistEnCode(entity.getId(), entity.getEnCode())) {
            return ActionResult.fail(MsgCode.EXIST002.get());
        }
        systemService.create(entity);
        return ActionResult.success(MsgCode.SU001.get());
    }

    /**
     * 修改系统
     *
     * @param id 主键
     * @param systemCrModel 修改模型
     * @return ignore
     */
    @ApiOperation("修改系统")
    @PutMapping("/{id}")
    public ActionResult update(@PathVariable("id") String id, @RequestBody SystemCrModel systemCrModel) {
        SystemEntity systemEntity = systemService.getInfo(id);
        if (systemEntity == null) {
            return ActionResult.fail(MsgCode.FA002.get());
        }
        SystemEntity entity = JsonUtil.getJsonToBean(systemCrModel, SystemEntity.class);
        entity.setIsMain(systemEntity.getIsMain());
        if (systemService.isExistFullName(id, entity.getFullName())) {
            return ActionResult.fail(MsgCode.EXIST001.get());
        }
        if (systemService.isExistEnCode(id, entity.getEnCode())) {
            return ActionResult.fail(MsgCode.EXIST002.get());
        }
        systemService.update(id, entity);
        return ActionResult.success(MsgCode.SU004.get());
    }

    /**
     * 删除系统
     *
     * @param id
     * @return ignore
     */
    @ApiOperation("删除系统")
    @DeleteMapping("/{id}")
    public ActionResult delete(@PathVariable("id") String id) {
        SystemEntity entity = systemService.getInfo(id);
        if (entity == null) {
            return ActionResult.fail(MsgCode.FA003.get());
        }
        if (entity.getIsMain() == 1) {
            return ActionResult.fail("主系统不允许删除");
        }
        systemService.delete(id);
        return ActionResult.success(MsgCode.SU003.get());
    }

}
